#pragma once
#include <DataStorageBase.h>
#include <mail.h>
#include <module_header_reg.h>
#include "module_general_reg.h"
#include "Scheduler.h"
#include "I2CSlave.h"
//---------------------- 40字节--------------------------------
#define DATA_STORAGE_HEADER_REG_SIZE sizeof(module_header_reg_t)
//---------------------- 24字节--------------------------------
#define DATA_STORAGE_GENERAL_REG_SIZE sizeof(module_general_reg_t)

#define DATA_STORAGE_STATUS_EOF_ADDRESS   (int)(DATA_STORAGE_GENERAL_REG_SIZE+DATA_STORAGE_HEADER_REG_SIZE)



class DataStorageCM : public DataStorageBase, public Scheduler
{
public:
    DataStorageCM(Mail<>& mb,I2CSlave& i2c_slave);
    
    void make_default() override;

    bool load() override;

    int read_data_storage_eof() override;
    
    void check_update() override;

    module_header_reg_t& module_header_reg() override
    {
        return DataStorageCM::module_reg().module_header_reg;
    }

    data_storage_reg_read_t& get_reg_data(uint8_t reg_address) override;
    
    
    static module_reg_t& module_reg()
    {
        static module_reg_t module_addressable_led_reg;
        return module_addressable_led_reg;
    }


    void schedule() override;
    void update() override;
    void cleanup()override;
protected:

    constexpr uint32_t data_storage_header_size()
    {
        return sizeof(module_header_reg_t);
    }
    constexpr uint32_t data_storage_general_size()
    {
        return sizeof(module_general_reg_t);
    }

private:
    Mail<>& _mb;
    I2CSlave& _i2c_slave;
    data_storage_eof_t _data_storage_eof;
    data_storage_reg_read_t _reg_read;
    os::memory_pool_t::memory_node_t _master_data_read;
};

